package net.csdra;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * Created by msf on 2018/1/24.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    String hostname;

    @Override
    public void configure(HttpSecurity web) throws Exception {
        web
                .headers().frameOptions().sameOrigin()
                .and()
                .csrf().disable().logout()
                .and().exceptionHandling().accessDeniedHandler((httpServletRequest, httpServletResponse, e) -> {
            httpServletResponse.sendRedirect(hostname + "/login?successUrl=" + httpServletRequest.getRequestURI());

        });

    }
}
